package controller

import (
	"danmu/common"
	"danmu/dto"
	"danmu/model"
	"danmu/response"
	"net/http"
	"path"
	"strconv"

	"github.com/gin-gonic/gin"
)

func UploadVideoInfo(ctx *gin.Context) {
	DB := common.GetDB()
	//从参数中获取标题和简介
	var requestVideo = model.Video{}
	err := ctx.Bind(&requestVideo)
	if err != nil {
		response.Response(ctx, http.StatusBadRequest, 400, nil, "请求错误")
		return
	}
	title := requestVideo.Title
	coverName := requestVideo.CoverName
	videoName := requestVideo.VideoName
	content := requestVideo.Content
	uid := requestVideo.Uid
	show := requestVideo.IsShow
	//验证数据
	if len(title) == 0 {
		response.Response(ctx, http.StatusUnprocessableEntity, 422, nil, "标题不能为空")
		return
	}
	if len(coverName) == 0 {
		response.Response(ctx, http.StatusUnprocessableEntity, 422, nil, "封面不存在")
		return
	}
	if len(videoName) == 0 {
		response.Response(ctx, http.StatusUnprocessableEntity, 422, nil, "视频不存在")
		return
	}
	if uid == 0 {
		response.Response(ctx, http.StatusUnprocessableEntity, 422, nil, "上传失败")
		return
	}
	//处理视频类型
	videoType := path.Ext(videoName)
	if videoType == ".mp4" {
		videoType = "mp4"
	} else {
		videoType = "flv"
	}
	newVideo := model.Video{
		Title:     title,
		CoverName: coverName,
		VideoName: videoName,
		VideoType: videoType,
		Content:   content,
		Uid:       uid,
	}
	DB.Create(&newVideo).Update("is_show", show)
	response.Success(ctx, nil, "上传成功")
}

func GetVideoList(ctx *gin.Context) {
	DB := common.GetDB()
	var video []model.Video
	uid := ctx.DefaultQuery("uid", "0")
	if uid == "0" {
		response.Fail(ctx, nil, "用户不存在")
		return
	}
	//获取分页信息
	page, _ := strconv.Atoi(ctx.Query("page"))
	pageSize, _ := strconv.Atoi(ctx.Query("page_size"))
	if page > 0 && pageSize > 0 {
		//记录总数
		var totalSize int
		//分页
		DB = DB.Limit(pageSize).Offset((page - 1) * pageSize)
		//根据id查找用户
		DB.First(&video, uid)
		DB.Where("uid = ? AND is_show = 1", uid).Find(&video).Count(&totalSize)
		response.Success(ctx, gin.H{"count": totalSize, "data": dto.ToVideoDto(video)}, "获取成功")
	} else {
		response.Fail(ctx, nil, "获取失败")
		return
	}
}

//获取自己的视频
func GetMineVideo(ctx *gin.Context) {
	DB := common.GetDB()
	var video []model.Video
	uid, _ := ctx.Get("id")
	//获取分页信息
	page, _ := strconv.Atoi(ctx.Query("page"))
	pageSize, _ := strconv.Atoi(ctx.Query("page_size"))
	if page > 0 && pageSize > 0 {
		//记录总数
		var totalSize int
		//分页
		DB = DB.Limit(pageSize).Offset((page - 1) * pageSize)
		//根据id查找用户
		DB.First(&video, uid)
		DB.Where("uid = ?", uid).Find(&video).Count(&totalSize)
		response.Success(ctx, gin.H{"count": totalSize, "data": dto.ToVideoDto(video)}, "获取成功")
	} else {
		response.Fail(ctx, nil, "获取失败")
		return
	}
}

func ModifyVideo(ctx *gin.Context) {
	DB := common.GetDB()
	var video model.Video
	//获取参数
	var requestVideo = model.Video{}
	err := ctx.Bind(&requestVideo)
	if err != nil {
		response.Response(ctx, http.StatusBadRequest, 400, nil, "请求错误")
		return
	}
	id := requestVideo.ID
	title := requestVideo.Title
	content := requestVideo.Content
	show := requestVideo.IsShow

	if len(title) == 0 || len(title) == 0 {
		response.Response(ctx, http.StatusUnprocessableEntity, 422, nil, "数据有误")
		return
	}
	DB.First(&video, id)
	if video.ID == 0 {
		response.Fail(ctx, nil, "视频不存在")
		return
	}
	//验证作者获取ID
	uid, _ := ctx.Get("id")
	if video.Uid != uid {
		ctx.JSON(http.StatusUnauthorized, gin.H{"code": 401, "msg": "权限不足"})
		return
	}
	DB.Debug().Model(&video).Updates(model.Video{Title: title, Content: content}).Update("is_show", show)
	response.Success(ctx, nil, "修改成功")
}

func GetVideoInfo(ctx *gin.Context) {
	DB := common.GetDB()
	var video = model.Video{}
	vid := ctx.DefaultQuery("vid", "0")
	if vid == "0" {
		response.Fail(ctx, nil, "视频不存在")
		return
	}
	//根据id查找视频
	DB.Model(&video).Where("id = ?", vid).Preload("Author").First(&video)
	if video.ID == 0{
		response.Fail(ctx, nil, "视频不存在")
		return
	}
	response.Success(ctx, gin.H{"data": dto.ToOneVideoDto(video)}, "获取成功")
}

func DeleteVideo(ctx *gin.Context) {
	DB := common.GetDB()
	//从参数中获取id
	var requestVideo = model.Video{}
	var video = model.Video{}
	err := ctx.Bind(&requestVideo)
	if err != nil {
		response.Response(ctx, http.StatusBadRequest, 400, nil, "请求错误")
		return
	}
	id := requestVideo.ID
	//根据id查找视频
	DB.First(&video, id)
	if video.ID == 0 {
		response.Fail(ctx, nil, "视频不存在")
		return
	}
	//验证作者获取ID
	uid, _ := ctx.Get("id")
	if video.Uid != uid {
		ctx.JSON(http.StatusUnauthorized, gin.H{"code": 401, "msg": "权限不足"})
		return
	}
	DB.Delete(&video)
	response.Success(ctx, nil, "删除成功")
}

func GetRecommend(ctx *gin.Context) {
	DB := common.GetDB()
	var video []model.Video
	page, _ := strconv.Atoi(ctx.Query("page"))
	pageSize, _ := strconv.Atoi(ctx.Query("page_size"))
	if page > 0 && pageSize > 0 {
		//记录总数
		var totalSize int
		DB = DB.Limit(pageSize).Offset((page - 1) * pageSize)
		DB.Where("is_show = 1").Find(&video).Count(&totalSize)
		response.Success(ctx, gin.H{"count": totalSize, "data": dto.ToVideoDto(video)}, "获取成功")
	}
}
